package com.lvideo.component.extraplayer.videoview;

import android.content.Context;
import android.media.MediaPlayer;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;

import com.letv.sdk.callbacks.LetvPlayerView;
import com.lvideo.component.extraplayer.Interface.MyAdStatusListener;
import com.lvideo.component.extraplayer.LVideoMediaPlayerControl;
import com.lvideo.component.extraplayer.R;
import com.lvideo.component.extraplayer.utils.MyPlayerConstant;
import com.media.ffmpeg.FFMpegPlayer;
import com.novaplayer.LetvMediaPlayerControl;
import com.novaplayer.LetvVideoViewBuilder;
import com.novaplayer.info.PlayUrl;
import com.novaplayer.listener.OnSubtitleListener;
import com.novaplayer.listener.OnVideoViewStateChangeListener;
import com.tencent.qqlive.mediaplayer.api.TVK_PlayerVideoInfo;
import com.tencent.qqlive.mediaplayer.api.TVK_UserInfo;

import java.util.List;
import java.util.Map;

/**
 * Created by zhaopeipei on 2017/5/25.
 */
public class VideoViewPasterProxy implements LVideoMediaPlayerControl {

    private Context mContext;

    private boolean mIsPlayingAd;

    private LetvMediaPlayerControl mPasterVideoPlayer;

    private View mPhotoView;

    private MediaPlayer.OnPreparedListener mOnPreparedListener;
    private MediaPlayer.OnSeekCompleteListener mOnSeekCompleteListener;
    private MediaPlayer.OnCompletionListener mOnCompletionListener;
    private MediaPlayer.OnErrorListener mOnErrorListener;
    private MediaPlayer.OnInfoListener mOnInfoListener;
    private MyAdStatusListener mOnAdStatusListener;

    private MediaPlayer.OnVideoSizeChangedListener mOnVideoSizeChangedListener;
    private MediaPlayer.OnBufferingUpdateListener mOnBufferingUpdateListener;
    private OnVideoViewStateChangeListener mOnVideoViewStateChangeListener;

    private int firstErrorCode = 0;

    private int mType;

    private int mCurrrentPosition;

    public VideoViewPasterProxy(Context context){
        this.mContext = context;
    }

    public void createPlayer(int type) {
        switch (type) {
            case MyPlayerConstant.MOBILE_MY_M3U8:
                mPasterVideoPlayer = LetvVideoViewBuilder.getInstants().build(mContext, LetvVideoViewBuilder.Type.SW_COMMON);
                mType = MyPlayerConstant.MOBILE_MY_M3U8;
                break;
            case MyPlayerConstant.MOBILE_MY_PHOTO:
                mPhotoView = View.inflate(mContext, R.layout.attach_photo ,null);
                mType = MyPlayerConstant.MOBILE_MY_PHOTO;
            default:
                mPasterVideoPlayer = LetvVideoViewBuilder.getInstants().build(mContext, LetvVideoViewBuilder.Type.DEFAULT);
        }
    }

    public void setPlayingAd(boolean isPlayingAd) {
        mIsPlayingAd = isPlayingAd;
    }

    public ImageView getAttachPhoto() {
        if (mPhotoView != null) {
            return (ImageView)(mPhotoView.findViewById(R.id.iv_attach_photo));
        }
        return null;
    }

    public boolean isPlayingAd() {
        return true;
    }

    public boolean isPlayingPhoto() {
        if (mType == MyPlayerConstant.MOBILE_MY_PHOTO) {
            return true;
        }
        return false;
    }


    @Override
    public void setVideoPlayUrl(PlayUrl url) {
    }

    @Override
    public void setVideoPath(String videoPath) {
        mPasterVideoPlayer.setVideoPath(videoPath);
    }

    @Override
    public void setVideoPath(String videoPath, Map<String, String> headers) {

    }

    @Override
    public View getView() {
        if (mType == MyPlayerConstant.MOBILE_MY_PHOTO) {
            return mPhotoView;
        }
        return mPasterVideoPlayer.getView();
    }

    @Override
    public boolean isSohuAdvertInPlayback() {
        return true;
    }

    @Override
    public void start() {
        try {
            if (mType == MyPlayerConstant.MOBILE_MY_PHOTO) {
                if (mOnAdStatusListener != null) {
                    mOnAdStatusListener.onAdLoading();
                }
            } else if (mPasterVideoPlayer != null) {
                mPasterVideoPlayer.start();
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public void complete() {
        if (mType == MyPlayerConstant.MOBILE_MY_PHOTO) {
            if (mOnAdStatusListener != null) {
                mOnAdStatusListener.onAdFinished();
            }
        }
    }

    @Override
    public void next() {
    }

    @Override
    public void setTencentData(TVK_UserInfo userinfo, TVK_PlayerVideoInfo playerinfo, int currPlayTime) {

    }

    @Override
    public void setLetvScreenView(LetvPlayerView letvScreenView) {

    }

    @Override
    public void setLetvData(long vid,int currPlayTime) {

    }

    @Override
    public void setBestvData(String vid, String fdnCode, long currPlayTime) {

    }

    @Override
    public int getFirstErrorCode() {
        return firstErrorCode;
    }

    @Override
    public void playVideoAt(int index) {
    }


    @Override
    public void pause() {
        if (mType == MyPlayerConstant.MOBILE_MY_PHOTO) {
            return;
        } else if (mPasterVideoPlayer != null) {
            mPasterVideoPlayer.pause();
        }
    }

    @Override
    public int getDuration() {
        if (mType == MyPlayerConstant.MOBILE_MY_PHOTO) {
            return 5000;
        }
        return mPasterVideoPlayer.getDuration();
    }

    @Override
    public int getCurrentPosition() {
        if (mType == MyPlayerConstant.MOBILE_MY_PHOTO) {
            return mCurrrentPosition;
        }
        return mPasterVideoPlayer.getCurrentPosition();
    }

    public void setCurrentPosition(int position) {
        mCurrrentPosition = position;
    }

    @Override
    public void stopPlayback() {
    }

    @Override
    public void seekTo(int pos) {
        mPasterVideoPlayer.seekTo(pos);
    }

    @Override
    public boolean isPlaying() {
        return true;
    }

    @Override
    public int getBufferPercentage() {
        return 0;
    }

    @Override
    public boolean canSeekForward() {
        return true;
    }

    @Override
    public int getAudioSessionId() {
        return 0;
    }

    @Override
    public boolean canSeekBackward() {
        return true;
    }

    @Override
    public void forward() {

    }

    @Override
    public void rewind() {

    }

    @Override
    public void adjust(int type) {

    }

    @Override
    public MediaPlayer getMediaPlayer() {
        return mPasterVideoPlayer.getMediaPlayer();
    }


    @Override
    public int getLastSeekWhenDestoryed() {
        return 0;
    }

    @Override
    public boolean isPaused() {
        return false;
    }

    @Override
    public boolean canPause() {
        return true;
    }

    @Override
    public void setMediaController(MediaController var1) {

    }

    @Override
    public boolean isInPlaybackState() {
        return false;
    }

    @Override
    public void setOnPreparedListener(MediaPlayer.OnPreparedListener var1) {
        if (isPlayingAd()) {
            mPasterVideoPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    if (mOnAdStatusListener != null) {
                        mOnAdStatusListener.onAdLoading();
                    }
                }
            });
        } else {
            mPasterVideoPlayer.setOnPreparedListener(var1);
        }
    }

    @Override
    public void setOnCompletionListener(MediaPlayer.OnCompletionListener var1) {
        if (isPlayingAd()) {
            mPasterVideoPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    if (mOnAdStatusListener != null) {
                        mOnAdStatusListener.onAdFinished();
                    }
                }
            });
        } else {
            mPasterVideoPlayer.setOnCompletionListener(var1);
        }
    }

    @Override
    public void setOnInfoListener(MediaPlayer.OnInfoListener var1) {
        mPasterVideoPlayer.setOnInfoListener(var1);
    }

    @Override
    public void setOnErrorListener(MediaPlayer.OnErrorListener var1) {
        mPasterVideoPlayer.setOnErrorListener(var1);
    }

    @Override
    public void setOnAdListener(MyAdStatusListener var) {
        mOnAdStatusListener = var;
    }

    @Override
    public void setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener var1) {
        mPasterVideoPlayer.setOnSeekCompleteListener(var1);
    }

    @Override
    public void setOnVideoSizeChangedListener(MediaPlayer.OnVideoSizeChangedListener var1) {
        this.mOnVideoSizeChangedListener = var1;
    }

    @Override
    public void setOnBufferingUpdateListener(MediaPlayer.OnBufferingUpdateListener l) {
        this.mOnBufferingUpdateListener = l;
    }

    @Override
    public void setVideoViewStateChangeListener(OnVideoViewStateChangeListener videoViewStateChangeListener) {
        mOnVideoViewStateChangeListener = videoViewStateChangeListener;
    }

    @Override
    public void setOnSubtitleListener(OnSubtitleListener onSubtitleListener) {

    }

    @Override
    public void setInitPosition(int i) {

    }

    @Override
    public void switchStreamSource(String s, int i, int i1) {

    }

    @Override
    public int setSourceType(int i) {
        return 0;
    }

    @Override
    public void setParameter(int i, Object o) {

    }

    @Override
    public boolean handleTouchEvent(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void usingSensor(int i, boolean b) {

    }

    @Override
    public void setLanguage(String s) {

    }

    @Override
    public String[] getLanguage() {
        return new String[0];
    }

    @Override
    public void setOnAdNumberListener(FFMpegPlayer.OnAdNumberListener var1) {

    }

    @Override
    public void setOnBlockListener(FFMpegPlayer.OnBlockListener var1) {

    }

    @Override
    public void setOnHardDecodeErrorListener(FFMpegPlayer.OnHardDecodeErrorListner var1) {

    }

    @Override
    public boolean isEnforcementWait() {
        return false;
    }

    @Override
    public void setEnforcementWait(boolean var1) {

    }

    @Override
    public boolean isEnforcementPause() {
        return false;
    }

    @Override
    public void setEnforcementPause(boolean var1) {

    }

    @Override
    public void setCacheSize(int var1, int var2, int var3, int var4) {

    }

    public List<Integer> getSupportDefinitions() {
        return null;
    }

    @Override
    public void changeDefinition(int definition){

    }

    @Override
    public int getCurrentDefinition() {
        return 0;
    }

    @Override
    public void onDestroyData() {
        mPasterVideoPlayer = null;
    }
}
